package com.zmzncs.lmtc.module.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zmzncs.lmtc.common.util.WeChatUtil;
import com.zmzncs.lmtc.common.pojo.LoginUser;
import com.zmzncs.lmtc.common.pojo.PageVO;
import com.zmzncs.lmtc.common.pojo.Rest;
import com.zmzncs.lmtc.common.util.JsonUtil;
import com.zmzncs.lmtc.common.util.JwtUtil;
import com.zmzncs.lmtc.common.util.RestUtil;
import com.zmzncs.lmtc.module.order.dao.ParkReserveRecordDao;
import com.zmzncs.lmtc.module.order.pojo.order_reserve.*;
import com.zmzncs.lmtc.module.order.service.ParkReserveRecordService;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 预约记录表 前端控制器
 * </p>
 *
 * @author 任建波
 * @since 2020-12-14
 */
@Api(tags = {"预约记录"})
@RestController
@RequestMapping("/order-reserve/park-reserve-record")
@Slf4j
public class ParkReserveRecordController {

    @Autowired
    private ParkReserveRecordService parkReserveRecordService;
    @Autowired
    private ParkReserveRecordDao parkReserveRecordDao;
    @Autowired
    HttpServletRequest request;

    @ApiOperation(value = "预约")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping("reserve")
    public Rest reserve(@RequestBody @Valid ParkReserveRecordAddForm form){
        Map<String, Object> reserve = parkReserveRecordService.reserve(form);

        return RestUtil.success(reserve);
    }


    @ApiOperation(value = "预约支付通知")
    @PostMapping(value = "pay-reserve-notice")
    public String payReserveNotice(HttpServletRequest request) {
        String callbackStr = WeChatUtil.parseCallback(request);
        log.info("预约支付通知：{}", callbackStr);
        if (StringUtils.isBlank(callbackStr)) {
            return WeChatUtil.wxpayCallbackResult(false);
        }

        String reserveNotice = parkReserveRecordService.payReserveNotice(callbackStr);

        return reserveNotice;
    }

    @ApiOperation(value = "取消预约")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @PostMapping(value = "cancel-reserve/{id}")
    public Rest cancelReserve(@PathVariable Long id) {
        return parkReserveRecordService.cancelReserve(id);
    }

//    @ApiOperation(value = "删除预约")
//    @PostMapping("delete/{id}")
//    public Rest delete(@PathVariable Long id){
//        parkReserveRecordService.delete(id);
//
//        return RestUtil.success();
//    }

//    @ApiOperation(value = "更新预约")
//    @PostMapping("update")
//    public Rest update(@RequestBody @Valid ParkReserveRecordUpdateForm form){
//        parkReserveRecordService.update(form);
//
//        return RestUtil.success();
//    }

    @ApiOperation(value = "查询预约记录信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name="pageNum", value="页码", required = true),
            @ApiImplicitParam(paramType="query", name="pageSize", value="页长", required = true),
    })
    @GetMapping("get-reserve-record")
    public Rest<IPage<ParkReserveRecordVO>> getReserveRecord(@RequestParam Integer pageNum, @RequestParam Integer pageSize,Integer status){
        IPage<ParkReserveRecordDTO> reserveRecord = parkReserveRecordService.getReserveRecord(pageNum, pageSize,status);
        PageVO<ParkReserveRecordVO> pageInfo = JsonUtil.pageTransform(reserveRecord, ParkReserveRecordVO.class);
        return RestUtil.success(pageInfo);
    }

    @ApiOperation(value = "查询当前成功的预约")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @GetMapping("reserved-curr")
    public Rest<List<ParkReserveRecordVO>> reservedCurr(){
        List<ParkReserveRecordDTO> parkReserveRecordDTOList = parkReserveRecordService.reservedCurr();
        List<ParkReserveRecordVO> parkReserveRecordVOList = JsonUtil.listTransform(parkReserveRecordDTOList, ParkReserveRecordVO.class);

        return RestUtil.success(parkReserveRecordVOList);
    }

    @ApiOperation(value = "根据id查询预约")
    @GetMapping("get-by-id")
    public Rest<ParkReserveRecordVO> getById(@RequestParam Long id){
        ParkReserveRecordDTO parkReserveRecordDTO = parkReserveRecordService.getById(id);
        ParkReserveRecordVO parkReserveRecordVO = new ParkReserveRecordVO();
        BeanUtils.copyProperties(parkReserveRecordDTO, parkReserveRecordVO);

        return RestUtil.success(parkReserveRecordVO);
    }

    @ApiOperation(value = "获取车主在某停车场未结束的预约")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
    })
    @GetMapping("get-unfinished-by-parkid")
    public Rest<List<ParkReserveRecordVO>> getUnfinishedByParkId(@RequestParam Long parkId){
        LoginUser loginUser = JwtUtil.getLoginUser(request);

        Integer[] statusList = new Integer[]{1, 2, 3};
        List<ParkReserveRecordDTO> reserveRecordDTOList = parkReserveRecordDao.getByParkIdStatus(loginUser.getCustomerId(), parkId, statusList);
        List<ParkReserveRecordVO> parkReserveRecordVOList = JsonUtil.listTransform(reserveRecordDTOList, ParkReserveRecordVO.class);

        return RestUtil.success(parkReserveRecordVOList);
    }

}
